
clear all
cd  "\\micro.intra\Projekt\P0624$\P0624_Gem\Kitschelt\submission"
log using setup_underlying, replace


*************************************************************************************************************
*************************************************************************************************************
****************Create dataset with ids for all politicians in main sample****************
************************Is used to restrict sample to relevant individuals*************************
*************************************************************************************************************
*************************************************************************************************************
use "\\micro.intra\Projekt\P0624$\P0624_Gem\Data Extraction and Files\Data files\politicians combined.dta", clear

keep if valar>=2006
keep p_id
duplicates drop p_id,force
save kit_polid, replace

*************************************************************************************************************
*************************************************************************************************************
****************Add various data on employment, income, education, occupation and family type****************
************************for all individuals from 2001 to 2012 and save as a new file*************************
*************************************************************************************************************
*************************************************************************************************************

odbc load, exec("select inc=DispInkPersF, Civil, FamTypF,  Sun2000niva, SektorKod, Ssyk4, p_id=Person_LopNr, AstSNI92, KU1InstKod7, KU1SektorKod, StudTyp, InstKod7, ForvErs, FoDelt, HSDelt, PasNar, FInk , YrkStalln, KU1YrkStalln, o_nr=Peorgnr_LopNr from P0624_Lisa_2001") dsn("P0624") clear
gen year =2001
save scale_, replace

forvalues year=2002/2003{	
odbc load, exec("select  inc=DispInkPersF, Civil, FamTypF,  Sun2000niva, SektorKod, Ssyk4, p_id=Person_LopNr, AstSNI2002, KU1InstKod7, KU1SektorKod, InstKod7, ForvErs, PasNar, FInk , YrkStalln, KU1YrkStalln, o_nr=Peorgnr_LopNr from P0624_Lisa_`year'") dsn("P0624") clear
gen year = `year'
append using scale_, force
save scale_, replace
}

forvalues year=2004/2006{	
odbc load, exec("select inc=DispInkPersF04, Civil, FamTypF,  Sun2000niva, SektorKod, OpFtgLedare, Ssyk4, p_id=Person_LopNr, KU1InstKod7, KU1SektorKod, ForvErs, AstSNI2002, InstKod7, KU1YrkStalln, PasNar, FInk=InkFNettoA, YrkStalln, o_nr=Peorgnr_LopNr from P0624_Lisa_`year'") dsn("P0624") clear
gen year =`year'
append using scale_, force
save scale_, replace
}

odbc load, exec("select inc=DispInkPersF04,  Civil, FamTypF,  Sun2000niva, SektorKod, OpFtgLedare, Ssyk4, p_id=Person_LopNr, KU1InstKod7, KU1SektorKod, ForvErs, AstSNI2002, AstSNI2007, InstKod7, KU1YrkStalln, PasNar, FInk=InkFNettoA, YrkStalln, o_nr=Peorgnr_LopNr from P0624_Lisa_2007") dsn("P0624") clear
gen year =2007
append using scale_, force
save scale_, replace

forvalues year=2008/2012{	
odbc load, exec("select inc=DispInkPersF04, Civil, FamTypF,  Sun2000niva, SektorKod, OpFtgLedare, Ssyk4, p_id=Person_LopNr, KU1InstKod7, KU1SektorKod, ForvErs, AstSNI2007, InstKod7, KU1YrkStalln, PasNar, FInk=InkFNettoA, YrkStalln, o_nr=Peorgnr_LopNr from P0624_Lisa_`year'") dsn("P0624") clear
gen year =`year'
append using scale_, force
save scale_, replace
}
compress

*********************************************************************************
*****************Define missing occupational codes as missing********************
*********************************************************************************
replace Ssyk4="" if Ssyk4=="0000" |Ssyk4=="****" 

*********************************************************************************************
***************Add data on gender and birthyear and remove all obs with age<18***************
*********************************************************************************************

joinby p_id using Birthyear_sex.dta, unmatched(none)
gen age = year - FodelseAr
gen age_ret=age>=65
drop if  age<18

******************************************************************
***************Add yearly data on price base amounts**************
*****************to be used as sample restriction*****************
******************************************************************

gen pbb=.
replace	pbb	=	44000	if 	year 	==	2012
replace	pbb	=	42800	if 	year 	==	2011
replace	pbb	=	42400	if 	year 	==	2010
replace	pbb	=	42800	if 	year 	==	2009
replace	pbb	=	41000	if 	year 	==	2008
replace	pbb	=	40300	if 	year 	==	2007
replace	pbb	=	39700	if 	year 	==	2006
replace	pbb	=	39400	if 	year 	==	2005
replace	pbb	=	39300	if 	year 	==	2004
replace	pbb	=	38600	if 	year 	==	2003
replace	pbb	=	37900	if 	year 	==	2002
replace	pbb	=	36900	if 	year 	==	2001
replace pbb=pbb/100 

**********************************************************************************
******************Define full-time politicians & party employees******************
**********************************************************************************

gen politician =  AstSNI2007 == "84111" | AstSNI2002 == "75111" | AstSNI92 == "75111"
gen party =  AstSNI2007 == "94920" | AstSNI2002 =="91320"  | AstSNI92 =="91320"



************************************************************************
******************Define employees and business people******************
************************************************************************
compress
gen BusPers=0 if YrkStalln!=""
replace BusPers=1 if YrkStalln=="4" |YrkStalln=="5" 
gen Employee=0 if YrkStalln!=""
replace Employee =1 if YrkStalln=="2" | YrkStalln=="1"

****************************************************************************************************
****************Estimate company size for all companies based number of observations****************
******************with the same organization number that have meaningful employment*****************
*********************(defined as having a salary above 3.5 price base amounts)**********************
****************************************************************************************************

gen count=1 if ForvErs>(pbb*3.5)
bysort year o_nr: egen ftgsize = sum(count)
replace ftgsize=0 if missing(o_nr)

************************************************************************************************************
****************Create capital ownership variable and assign values to individuals depending****************
******************on employment type (employee/r), ownership company size if owner/manager******************
**************Assign missing values for politicians, party workers and non-meaingfully employed*************
**************************(defined as having a salary below 3.5 price base amounts**************************
************************************************************************************************************

gen cap_exec = -1 if Employee==1
replace cap_exec = 0.5 if PasNar!=0 | FInk!=0
replace cap_exec = 0.7 if (OpFtgLedare =="1" | BusPers ==1) & ftgsize < 10
replace cap_exec = 0.8 if (OpFtgLedare =="1" | BusPers ==1) & ftgsize > 9 & ftgsize < 50
replace cap_exec = 0.9 if (OpFtgLedare =="1" | BusPers ==1) & ftgsize > 49 & ftgsize < 250
replace cap_exec = 1   if (OpFtgLedare =="1" | BusPers ==1) & ftgsize > 249

replace cap_exec=. if ForvErs<(pbb*3.5) |politician==1 | party==1
drop Empl- ftgsize 
gen missing= cap_exec==.
*************************************************************************************************************
*************************Create sector variable indicating public/private/non-profit*************************
**************Assign missing values for politicians, party workers and non-meaingfully employed**************
**************************(defined as having a salary below 3.5 price base amounts)**************************
*************************************************************************************************************

gen PrivSec =0 if KU1SektorKod!="00"
replace PrivSec=1 if KU1SektorKod=="21" |KU1SektorKod=="22" |KU1SektorKod=="25"
gen PubSec = (PrivSec-1)*-1
gen PubCom = 0 if KU1SektorKod!="00"
replace PubCom = 1 if KU1SektorKod=="12" |KU1SektorKod=="23" |KU1SektorKod=="24"
replace PubSec=0 if PubCom==1
compress
gen NonProfit = 0 if KU1SektorKod!="00"
gen InstKodsub = substr(KU1InstKod7, 6,7)
replace NonProfit=1 if InstKodsub == "54" | InstKodsub == "61" | InstKodsub == "62" | InstKodsub == "63" | InstKodsub== "71" | InstKodsub== "72"| InstKodsub== "95"
replace PrivSec = 0 if NonProfit ==1
gen  sector = PrivSec - PubSec - PubCom*0 - NonProfit*0
replace sector=. if ForvErs<(pbb*3.5)|politician==1 | party==1
replace missing=1 if sector==.
drop KU1* InstKodsub SektorKod Ast*

************************************************************************************************************
*****************Generate income percentiles for individuals who are not full-time politicians**************
*****************Party employees or have non-meaningful employment. Separate for employees.*****************
************************************************************************************************************

compress
fasterxtile inc_pct=ForvErs if  ForvErs>(pbb*3.5)& politician!=1 & party!=1, by(year) n(100)
fasterxtile inc_pct_emp=ForvErs if  ForvErs>(pbb*3.5) & cap_exec ==-1 , by(year) n(100)


******************************************************************************************************
***********Impute variable values for full-time politicians, party employees and those with***********
********non-meaningful employment from previous non-missing observation of the same individual********
******************************************************************************************************

sort p_id year
foreach var in Ssyk4{
	replace `var'="" if ForvErs<(pbb*3.5) |politician==1 | party==1
	replace missing=1 if `var'==""
	replace `var'=`var'[_n-1] if `var'=="" &  p_id==p_id[_n-1]
}
foreach var in sector   inc_pct inc_pct_emp  cap_exec {
replace missing=1 if `var'==.
	replace `var'=`var'[_n-1] if `var'==. &  p_id==p_id[_n-1]
	
}
destring OpFtgLedare , replace force
replace OpFtgLedare=0 if OpFtgLedare==. & year >2003
foreach var of varlist PrivSec-NonProfit OpFtgLedare BusPers PasNar FInk{
	replace `var'=. if ForvErs<(pbb*3.5) |politician==1 | party==1
	replace missing=1 if `var'==.
	replace `var'=`var'[_n-1] if `var'==. &  p_id==p_id[_n-1]
	
}

***********************************************************************
******Mark observations with incomes below 3.5 prise base amounts******
**************************and save data********************************
***********************************************************************

gen lowinc=ForvErs<(pbb*3.5)
save base, replace

***************************************************************
*******Remove years that are not used in future analyses*******
*********************and drop dupliceates**********************
***************************************************************

keep if year == 2003 | year == 2006 | year == 2010 | year == 2012 

duplicates drop p_id year, force


***************************************************************
**************Add data on occupational offshoring**************
***************************************************************

gen ssyk96kod = substr(Ssyk4,1,3)
merge m:1 ssyk96kod using "intcomp_ssyk.dta"
drop ssyk96kod


********************************************************************
***********Generate variable meausring years of education***********
********************************************************************

replace Sun2000niva="" if Sun2000niva=="*" | Sun2000niva=="-"
destring Sun2000niva, replace force
replace  Sun2000niva=. if Sun2000niva>=999
gen educ_year=.
replace educ_year= 7.5 if Sun2000niva>=100 & Sun2000niva<200
replace educ_year= 9.4 if Sun2000niva>=200 & Sun2000niva<300
replace educ_year= 11.2 if Sun2000niva>=300 & Sun2000niva<330
replace educ_year= 12.4 if Sun2000niva>=330 & Sun2000niva<400
replace educ_year= 14.2 if Sun2000niva>=410& Sun2000niva<530
replace educ_year= 17 if Sun2000niva>=530 & Sun2000niva<600
replace educ_year= 20.4 if Sun2000niva>=600 & Sun2000niva<=999
drop Sun*

*************************************************************
***********Generate categorical education variable***********
*************************************************************

gen int eduyrs = educ_year
gen education = .
replace education = -1 if eduyrs<10
replace education = -.5 if eduyrs==11
replace education = -.25 if eduyrs==12
replace education = 0.5 if eduyrs==14
replace education = 1 if eduyrs>15
replace education = . if missing(eduyrs)

***************************************************************
*********Add data on children and civil state variable********* 
***************************************************************
drop _merge
joinby p_id using "\\micro.intra\Projekt\P0624$\P0624_Gem\Data Extraction and Files\Data files\child_young_old.dta" , unmatched(master)

gen child= birthyear_oldch<=year

gen married=Civil=="G "
gen partner =FamTyp=="11" | FamTyp=="21" |  ((FamTyp=="12" | FamTyp=="13"| FamTyp=="22" | FamTyp=="23") & child==1)
gen single=married!=1 & partner!=1

***************************************************************
**********Add data on occupational characteristics*************
**********expected to socialise gal-tan preferences************
**********and standardize average into one variable************
***************************************************************

gen  ssyk96kod = Ssyk4
drop _merge
joinby ssyk96kod using "SSYK96_Onet.dta", unmatched(master)
drop _merge


foreach var of varlist ConMacProc DocRecInfo HandMoveObj InterComp AdminAct{
gen `var'_ = 100-`var'
}
egen galtan = rowmean(ConcOther AssCareOther NegConSolv ServOriCare SocPercep ConMacProc_ DocRecInfo_ HandMoveObj_ InterComp_ AdminAct_) 
replace galtan=. if Ssyk4==""

foreach var in ConcOther AssCareOther NegConSolv ServOriCare SocPercep ConMacProc_ DocRecInfo_ HandMoveObj_ InterComp_ AdminAct_{
	egen std_`var'=std(`var')
}

egen galtan_std = rowmean(std_ConcOther-std_AdminAct_) 

********************************************************************************
***********Assign centered variable values based on ranked percentiles**********
***********for offshoring, occupational characteristics and income**************
********************************************************************************

foreach var in  galtan_std   stdoffshore galtan{
fastxtile `var'_pct = `var', n(100)
replace  `var'_pct = (`var'_pct-50.5)/49.5
}

drop std_ConcOther-std_AdminAct_
foreach var in  inc_pct_emp  inc_pct{

replace  `var' = (`var'-50.5)/49.5
}

replace inc_pct_emp= inc_pct_emp/(2/1.5)-.25

****************************************************************
*****Generate alternative capital variable with values for******
********non-capital owners replaced by income percentile********
****************************************************************

gen cap_exec_alt= cap_exec
replace  cap_exec_alt= inc_pct_emp if cap_exec==-1

**************************************************************
**************Combine subcomponent variable into**************
***********x and y index variables with variations************
**************************************************************

gen y = galtan_pct/2 + education/4 + (Kon-1.5)/2
gen y_gaw = galtan_std_pct/2 + education/4 + (Kon-1.5)/2
gen x = stdoffshore_pct/3+ sector/3 + cap_exec_alt/3 
gen x_alt = inc_pct/3+ sector/3 + cap_exec/3
gen x_alt2 = stdoffshore_pct/3+ sector/3 + cap_exec_alt/3 
drop if year <2006
drop if x==. |y==.
gen pasnar_d=PasNar!=0
gen fink_d=FInk!=0
gen busink=FInk!=0 | PasNar!=0
save main_data_pop, replace



********************************************************************************
********Generate index variables besed on the average for all years ************
********that we can observe the index values for an invdivudal  ************
********************************************************************************

cd "\\micro.intra\Projekt\P0624$\P0624_Gem\Kitschelt\submission"
use base.dta, replace
duplicates drop p_id year, force

gen ssyk96kod = substr(Ssyk4,1,3)

merge m:1 ssyk96kod using intcomp_ssyk.dta
drop ssyk96kod

replace Sun2000niva="" if Sun2000niva=="*" | Sun2000niva=="-"
destring Sun2000niva, replace force
replace  Sun2000niva=. if Sun2000niva>=999
gen educ_year=.
replace educ_year= 7.5 if Sun2000niva>=100 & Sun2000niva<200
replace educ_year= 9.4 if Sun2000niva>=200 & Sun2000niva<300
replace educ_year= 11.2 if Sun2000niva>=300 & Sun2000niva<330
replace educ_year= 12.4 if Sun2000niva>=330 & Sun2000niva<400
replace educ_year= 14.2 if Sun2000niva>=410& Sun2000niva<530
replace educ_year= 17 if Sun2000niva>=530 & Sun2000niva<600
replace educ_year= 20.4 if Sun2000niva>=600 & Sun2000niva<=999
drop Sun*

***NY UTBILDNINGSVARIABEL
gen int eduyrs = educ_year
gen education = .
replace education = -1 if eduyrs<10
replace education = -.5 if eduyrs==11
replace education = -.25 if eduyrs==12
replace education = 0.5 if eduyrs==14
replace education = 1 if eduyrs>15
replace education = . if missing(eduyrs)



gen  ssyk96kod = Ssyk4
drop _merge
joinby ssyk96kod using SSYK96_Onet.dta, unmatched(master)
drop _merge


foreach var of varlist ConMacProc DocRecInfo HandMoveObj InterComp AdminAct{
gen `var'_ = 100-`var'
}
egen galtan = rowmean(ConcOther AssCareOther NegConSolv ServOriCare SocPercep ConMacProc_ DocRecInfo_ HandMoveObj_ InterComp_ AdminAct_) 
replace galtan=. if Ssyk4==""


foreach var in    stdoffshore galtan{
fastxtile `var'_pct = `var', n(100)
replace  `var'_pct = (`var'_pct-50.5)/49.5
}

foreach var in  inc_pct_emp{

replace  `var' = (`var'-50.5)/49.5
}

replace inc_pct_emp= inc_pct_emp/(2/1.5)-.25


gen cap_exec_alt= cap_exec

replace  cap_exec_alt= inc_pct_emp if cap_exec==-1
gen y = galtan_pct/2 + education/4 + (Kon-1.5)/2
gen x = stdoffshore_pct/3+ sector/3 + cap_exec_alt/3 

keep y x  politician lowinc year p_id
gen noimput= politician!=1 & lowinc!=1
ren y y_avg
ren x x_avg 
gen  y_avg_ni = y_avg if noimput==1
gen x_avg_ni = x_avg if noimput==1
collapse x* y*, by(p_id)
foreach var in x_avg x_avg_ni y_avg y_avg_ni{
	egen std_`var'=std(`var')
}
save index_avg, replace


********************************************************************************
********Generate index variables with sample restricted to those earning********
********below 0.5 price base amounts instead of 3.5.****************************
********Same coding as complete setup (main data) with larger sample************
********************************************************************************


clear all
cd  "\\micro.intra\Projekt\P0624$\P0624_Gem\Kitschelt\submission"
use scale_, replace

*********************************************************************************
*****************Define missing occupational codes as missing********************
*********************************************************************************
replace Ssyk4="" if Ssyk4=="0000" |Ssyk4=="****" 

*********************************************************************************************
***************Add data on gender and birthyear and remove all obs with age<18***************
*********************************************************************************************

joinby p_id using Birthyear_sex.dta, unmatched(none)
gen age = year - FodelseAr
drop if  age<18

******************************************************************
***************Add yearly data on price base amounts**************
*****************to be used as sample restriction*****************
******************************************************************

gen pbb=.
replace	pbb	=	44000	if 	year 	==	2012
replace	pbb	=	42800	if 	year 	==	2011
replace	pbb	=	42400	if 	year 	==	2010
replace	pbb	=	42800	if 	year 	==	2009
replace	pbb	=	41000	if 	year 	==	2008
replace	pbb	=	40300	if 	year 	==	2007
replace	pbb	=	39700	if 	year 	==	2006
replace	pbb	=	39400	if 	year 	==	2005
replace	pbb	=	39300	if 	year 	==	2004
replace	pbb	=	38600	if 	year 	==	2003
replace	pbb	=	37900	if 	year 	==	2002
replace	pbb	=	36900	if 	year 	==	2001
replace pbb=pbb/100 

**********************************************************************************
******************Define full-time politicians & party employees******************
**********************************************************************************

gen politician =  AstSNI2007 == "84111" | AstSNI2002 == "75111" | AstSNI92 == "75111"
gen party =  AstSNI2007 == "94920" | AstSNI2002 =="91320"  | AstSNI92 =="91320"



************************************************************************
******************Define employees and business people******************
************************************************************************
compress
gen BusPers=0 if YrkStalln!=""
replace BusPers=1 if YrkStalln=="4" |YrkStalln=="5" 
gen Employee=0 if YrkStalln!=""
replace Employee =1 if YrkStalln=="2" | YrkStalln=="1"

****************************************************************************************************
****************Estimate company size for all companies based number of observations****************
******************with the same organization number that have meaningful employment*****************
*********************(defined as having a income above 0.5 price base amounts)**********************
****************************************************************************************************

gen count=1 if ForvErs>(pbb*0.5)
bysort year o_nr: egen ftgsize = sum(count)
replace ftgsize=0 if missing(o_nr)

************************************************************************************************************
****************Create capital ownership variable and assign values to individuals depending****************
******************on employment type (employee/r), ownership company size if owner/manager******************
**************Assign missing values for politicians, party workers and non-meaingfully employed*************
**************************(defined as having a income below 0.5 price base amounts**************************
************************************************************************************************************

gen cap_exec = -1 if Employee==1
replace cap_exec = 0.5 if PasNar!=0 | FInk!=0
replace cap_exec = 0.7 if (OpFtgLedare =="1" | BusPers ==1) & ftgsize < 10
replace cap_exec = 0.8 if (OpFtgLedare =="1" | BusPers ==1) & ftgsize > 9 & ftgsize < 50
replace cap_exec = 0.9 if (OpFtgLedare =="1" | BusPers ==1) & ftgsize > 49 & ftgsize < 250
replace cap_exec = 1   if (OpFtgLedare =="1" | BusPers ==1) & ftgsize > 249

replace cap_exec=. if ForvErs<(pbb*0.5) |politician==1 | party==1
drop Empl- ftgsize 

*************************************************************************************************************
*************************Create sector variable indicating public/private/non-profit*************************
**************Assign missing values for politicians, party workers and non-meaingfully employed**************
**************************(defined as having a income below 0.5 price base amounts)**************************
*************************************************************************************************************

gen PrivSec =0 if KU1SektorKod!="00"
replace PrivSec=1 if KU1SektorKod=="21" |KU1SektorKod=="22" |KU1SektorKod=="25"
gen PubSec = (PrivSec-1)*-1
gen PubCom = 0 if KU1SektorKod!="00"
replace PubCom = 1 if KU1SektorKod=="12" |KU1SektorKod=="23" |KU1SektorKod=="24"
replace PubSec=0 if PubCom==1
compress
gen NonProfit = 0 if KU1SektorKod!="00"
gen InstKodsub = substr(KU1InstKod7, 6,7)
replace NonProfit=1 if InstKodsub == "54" | InstKodsub == "61" | InstKodsub == "62" | InstKodsub == "63" | InstKodsub== "71" | InstKodsub== "72"| InstKodsub== "95"
replace PrivSec = 0 if NonProfit ==1
gen  sector = PrivSec - PubSec - PubCom*0 - NonProfit*0
replace sector=. if ForvErs<(pbb*0.5)|politician==1 | party==1
drop KU1* InstKodsub SektorKod Ast*

************************************************************************************************************
*****************Generate income percentiles for individuals who are not full-time politicians**************
*****************Party employees or have non-meaningful employment. Separate for employees.*****************
************************************************************************************************************

compress
fasterxtile inc_pct=ForvErs if  ForvErs>(pbb*0.5)& politician!=1 & party!=1, by(year) n(100)
fasterxtile inc_pct_emp=ForvErs if  ForvErs>(pbb*0.5) & cap_exec ==-1 , by(year) n(100)


******************************************************************************************************
***********Impute variable values for full-time politicians, party employees and those with***********
********non-meaningful employment from previous non-missing observation of the same individual********
******************************************************************************************************

sort p_id year
foreach var in Ssyk4{
	replace `var'="" if ForvErs<(pbb*0.5) |politician==1 | party==1
	replace `var'=`var'[_n-1] if `var'=="" &  p_id==p_id[_n-1]
}
foreach var in sector   inc_pct inc_pct_emp  cap_exec {

	replace `var'=`var'[_n-1] if `var'==. &  p_id==p_id[_n-1]
}
destring OpFtgLedare , replace force
replace OpFtgLedare=0 if OpFtgLedare==. & year >2003
foreach var of varlist PrivSec-NonProfit OpFtgLedare BusPers PasNar FInk{
	replace `var'=. if ForvErs<(pbb*0.5) |politician==1 | party==1
	replace `var'=`var'[_n-1] if `var'==. &  p_id==p_id[_n-1]
}


***************************************************************
*******Remove years that are not used in future analyses*******
*********************and drop dupliceates**********************
***************************************************************

keep if year == 2003 | year == 2006 | year == 2010 | year == 2012 

duplicates drop p_id year, force


***************************************************************
**************Add data on occupational offshoring**************
***************************************************************

gen ssyk96kod = substr(Ssyk4,1,3)
merge m:1 ssyk96kod using "intcomp_ssyk.dta"
drop ssyk96kod


********************************************************************
***********Generate variable meausring years of education***********
********************************************************************

replace Sun2000niva="" if Sun2000niva=="*" | Sun2000niva=="-"
destring Sun2000niva, replace force
replace  Sun2000niva=. if Sun2000niva>=999
gen educ_year=.
replace educ_year= 7.5 if Sun2000niva>=100 & Sun2000niva<200
replace educ_year= 9.4 if Sun2000niva>=200 & Sun2000niva<300
replace educ_year= 11.2 if Sun2000niva>=300 & Sun2000niva<330
replace educ_year= 12.4 if Sun2000niva>=330 & Sun2000niva<400
replace educ_year= 14.2 if Sun2000niva>=410& Sun2000niva<530
replace educ_year= 17 if Sun2000niva>=530 & Sun2000niva<600
replace educ_year= 20.4 if Sun2000niva>=600 & Sun2000niva<=999
drop Sun*

*************************************************************
***********Generate categorical education variable***********
*************************************************************

gen int eduyrs = educ_year
gen education = .
replace education = -1 if eduyrs<10
replace education = -.5 if eduyrs==11
replace education = -.25 if eduyrs==12
replace education = 0.5 if eduyrs==14
replace education = 1 if eduyrs>15
replace education = . if missing(eduyrs)



***************************************************************
**********Add data on occupational characteristics*************
**********expected to socialise gal-tan preferences************
**********and standardize average into one variable************
***************************************************************

gen  ssyk96kod = Ssyk4
drop _merge
joinby ssyk96kod using "SSYK96_Onet.dta", unmatched(master)
drop _merge


foreach var of varlist ConMacProc DocRecInfo HandMoveObj InterComp AdminAct{
gen `var'_ = 100-`var'
}
egen galtan = rowmean(ConcOther AssCareOther NegConSolv ServOriCare SocPercep ConMacProc_ DocRecInfo_ HandMoveObj_ InterComp_ AdminAct_) 
replace galtan=. if Ssyk4==""

foreach var in ConcOther AssCareOther NegConSolv ServOriCare SocPercep ConMacProc_ DocRecInfo_ HandMoveObj_ InterComp_ AdminAct_{
	egen std_`var'=std(`var')
}

egen galtan_std = rowmean(std_ConcOther-std_AdminAct_) 

********************************************************************************
***********Assign centered variable values based on ranked percentiles**********
***********for offshoring, occupational characteristics and income**************
********************************************************************************

foreach var in  galtan_std   stdoffshore galtan{
fastxtile `var'_pct = `var', n(100)
replace  `var'_pct = (`var'_pct-50.5)/49.5
}

drop std_ConcOther-std_AdminAct_
foreach var in  inc_pct_emp  inc_pct{

replace  `var' = (`var'-50.5)/49.5
}

replace inc_pct_emp= inc_pct_emp/(2/1.5)-.25

****************************************************************
*****Generate alternative capital variable with values for******
********non-capital owners replaced by income percentile********
****************************************************************

gen cap_exec_alt= cap_exec
replace  cap_exec_alt= inc_pct_emp if cap_exec==-1

**************************************************************
**************Combine subcomponent variable into**************
***********x and y index variables with variations************
**************************************************************



gen y = galtan_pct/2 + education/4 + (Kon-1.5)/2
gen x = stdoffshore_pct/3+ sector/3 + cap_exec_alt/3 

 keep y x  year p_id

ren y y_05pbb
ren x x_05pbb 

keep if year>=2006

foreach var in x_05pbb y_05pbb{
	egen std_`var'=std(`var')
}
joinby p_id using "kit_polid.dta" , unmatched(none)
save index_05pbb, replace



********************************************************************************
******Generate index variables without any price base amount restrictions*******
******Same coding as complete setup (main data) with full sample****************
********************************************************************************

clear all
cd  "\\micro.intra\Projekt\P0624$\P0624_Gem\Kitschelt\submission"
use scale_, replace

*********************************************************************************
*****************Define missing occupational codes as missing********************
*********************************************************************************
replace Ssyk4="" if Ssyk4=="0000" |Ssyk4=="****" 

*********************************************************************************************
***************Add data on gender and birthyear and remove all obs with age<18***************
*********************************************************************************************

joinby p_id using Birthyear_sex.dta, unmatched(none)
gen age = year - FodelseAr

drop if  age<18

******************************************************************
***************Add yearly data on price base amounts**************
*****************to be used as sample restriction*****************
******************************************************************

gen pbb=.
replace	pbb	=	44000	if 	year 	==	2012
replace	pbb	=	42800	if 	year 	==	2011
replace	pbb	=	42400	if 	year 	==	2010
replace	pbb	=	42800	if 	year 	==	2009
replace	pbb	=	41000	if 	year 	==	2008
replace	pbb	=	40300	if 	year 	==	2007
replace	pbb	=	39700	if 	year 	==	2006
replace	pbb	=	39400	if 	year 	==	2005
replace	pbb	=	39300	if 	year 	==	2004
replace	pbb	=	38600	if 	year 	==	2003
replace	pbb	=	37900	if 	year 	==	2002
replace	pbb	=	36900	if 	year 	==	2001
replace pbb=pbb/100 

**********************************************************************************
******************Define full-time politicians & party employees******************
**********************************************************************************

gen politician =  AstSNI2007 == "84111" | AstSNI2002 == "75111" | AstSNI92 == "75111"
gen party =  AstSNI2007 == "94920" | AstSNI2002 =="91320"  | AstSNI92 =="91320"



************************************************************************
******************Define employees and business people******************
************************************************************************
compress
gen BusPers=0 if YrkStalln!=""
replace BusPers=1 if YrkStalln=="4" |YrkStalln=="5" 
gen Employee=0 if YrkStalln!=""
replace Employee =1 if YrkStalln=="2" | YrkStalln=="1"

****************************************************************************************************
****************Estimate company size for all companies based number of observations****************
******************with the same organization number that have meaningful employment*****************
*********************(defined as having a income above 3.5 price base amounts)**********************
****************************************************************************************************

gen count=1 if ForvErs>(pbb*3.5)
bysort year o_nr: egen ftgsize = sum(count)
replace ftgsize=0 if missing(o_nr)

************************************************************************************************************
****************Create capital ownership variable and assign values to individuals depending****************
******************on employment type (employee/r), ownership company size if owner/manager******************
**************Assign missing values for politicians, party workers and non-meaingfully employed*************
**************************(defined as having a income below 0.5 price base amounts**************************
************************************************************************************************************

gen cap_exec = -1 if Employee==1
replace cap_exec = 0.5 if PasNar!=0 | FInk!=0
replace cap_exec = 0.7 if (OpFtgLedare =="1" | BusPers ==1) & ftgsize < 10
replace cap_exec = 0.8 if (OpFtgLedare =="1" | BusPers ==1) & ftgsize > 9 & ftgsize < 50
replace cap_exec = 0.9 if (OpFtgLedare =="1" | BusPers ==1) & ftgsize > 49 & ftgsize < 250
replace cap_exec = 1   if (OpFtgLedare =="1" | BusPers ==1) & ftgsize > 249

replace cap_exec=. if politician==1 | party==1
drop Empl- ftgsize 

*************************************************************************************************************
*************************Create sector variable indicating public/private/non-profit*************************
**************Assign missing values for politicians, party workers and non-meaingfully employed**************
**************************(defined as having a income below 0.5 price base amounts)**************************
*************************************************************************************************************

gen PrivSec =0 if KU1SektorKod!="00"
replace PrivSec=1 if KU1SektorKod=="21" |KU1SektorKod=="22" |KU1SektorKod=="25"
gen PubSec = (PrivSec-1)*-1
gen PubCom = 0 if KU1SektorKod!="00"
replace PubCom = 1 if KU1SektorKod=="12" |KU1SektorKod=="23" |KU1SektorKod=="24"
replace PubSec=0 if PubCom==1
compress
gen NonProfit = 0 if KU1SektorKod!="00"
gen InstKodsub = substr(KU1InstKod7, 6,7)
replace NonProfit=1 if InstKodsub == "54" | InstKodsub == "61" | InstKodsub == "62" | InstKodsub == "63" | InstKodsub== "71" | InstKodsub== "72"| InstKodsub== "95"
replace PrivSec = 0 if NonProfit ==1
gen  sector = PrivSec - PubSec - PubCom*0 - NonProfit*0
replace sector=. if politician==1 | party==1
drop KU1* InstKodsub SektorKod Ast*



************************************************************************************************************
*****************Generate income percentiles for individuals who are not full-time politicians**************
*****************Party employees or have non-meaningful employment. Separate for employees.*****************
************************************************************************************************************

compress
fasterxtile inc_pct=ForvErs if  politician!=1 & party!=1, by(year) n(100)
fasterxtile inc_pct_emp=ForvErs if   cap_exec ==-1 , by(year) n(100)


******************************************************************************************************
***********Impute variable values for full-time politicians, party employees and those with***********
********non-meaningful employment from previous non-missing observation of the same individual********
******************************************************************************************************

sort p_id year
foreach var in Ssyk4{
	replace `var'="" if politician==1 | party==1
	replace `var'=`var'[_n-1] if `var'=="" &  p_id==p_id[_n-1]
}
foreach var in sector   inc_pct inc_pct_emp  cap_exec {

	replace `var'=`var'[_n-1] if `var'==. &  p_id==p_id[_n-1]
}
destring OpFtgLedare , replace force
replace OpFtgLedare=0 if OpFtgLedare==. & year >2003
foreach var of varlist PrivSec-NonProfit OpFtgLedare BusPers PasNar FInk{
	replace `var'=. if ForvErs<(pbb*0.5) |politician==1 | party==1
	replace `var'=`var'[_n-1] if `var'==. &  p_id==p_id[_n-1]
}

keep if year == 2003 | year == 2006 | year == 2010 | year == 2012 



duplicates drop p_id year, force


***************************************************************
**************Add data on occupational offshoring**************
***************************************************************

gen ssyk96kod = substr(Ssyk4,1,3)
merge m:1 ssyk96kod using "intcomp_ssyk.dta"
drop ssyk96kod


********************************************************************
***********Generate variable meausring years of education***********
********************************************************************

replace Sun2000niva="" if Sun2000niva=="*" | Sun2000niva=="-"
destring Sun2000niva, replace force
replace  Sun2000niva=. if Sun2000niva>=999
gen educ_year=.
replace educ_year= 7.5 if Sun2000niva>=100 & Sun2000niva<200
replace educ_year= 9.4 if Sun2000niva>=200 & Sun2000niva<300
replace educ_year= 11.2 if Sun2000niva>=300 & Sun2000niva<330
replace educ_year= 12.4 if Sun2000niva>=330 & Sun2000niva<400
replace educ_year= 14.2 if Sun2000niva>=410& Sun2000niva<530
replace educ_year= 17 if Sun2000niva>=530 & Sun2000niva<600
replace educ_year= 20.4 if Sun2000niva>=600 & Sun2000niva<=999
drop Sun*

*************************************************************
***********Generate categorical education variable***********
*************************************************************

gen int eduyrs = educ_year
gen education = .
replace education = -1 if eduyrs<10
replace education = -.5 if eduyrs==11
replace education = -.25 if eduyrs==12
replace education = 0.5 if eduyrs==14
replace education = 1 if eduyrs>15
replace education = . if missing(eduyrs)



***************************************************************
**********Add data on occupational characteristics*************
**********expected to socialise gal-tan preferences************
**********and standardize average into one variable************
***************************************************************

gen  ssyk96kod = Ssyk4
drop _merge
joinby ssyk96kod using "SSYK96_Onet.dta", unmatched(master)
drop _merge


foreach var of varlist ConMacProc DocRecInfo HandMoveObj InterComp AdminAct{
gen `var'_ = 100-`var'
}
egen galtan = rowmean(ConcOther AssCareOther NegConSolv ServOriCare SocPercep ConMacProc_ DocRecInfo_ HandMoveObj_ InterComp_ AdminAct_) 
replace galtan=. if Ssyk4==""

foreach var in ConcOther AssCareOther NegConSolv ServOriCare SocPercep ConMacProc_ DocRecInfo_ HandMoveObj_ InterComp_ AdminAct_{
	egen std_`var'=std(`var')
}

egen galtan_std = rowmean(std_ConcOther-std_AdminAct_) 

********************************************************************************
***********Assign centered variable values based on ranked percentiles**********
***********for offshoring, occupational characteristics and income**************
********************************************************************************

foreach var in  galtan_std   stdoffshore galtan{
fastxtile `var'_pct = `var', n(100)
replace  `var'_pct = (`var'_pct-50.5)/49.5
}

drop std_ConcOther-std_AdminAct_
foreach var in  inc_pct_emp  inc_pct{

replace  `var' = (`var'-50.5)/49.5
}

replace inc_pct_emp= inc_pct_emp/(2/1.5)-.25

****************************************************************
*****Generate alternative capital variable with values for******
********non-capital owners replaced by income percentile********
****************************************************************

gen cap_exec_alt= cap_exec
replace  cap_exec_alt= inc_pct_emp if cap_exec==-1

**************************************************************
**************Combine subcomponent variable into**************
***********x and y index variables with variations************
**************************************************************


gen y = galtan_pct/2 + education/4 + (Kon-1.5)/2
gen x = stdoffshore_pct/3+ sector/3 + cap_exec_alt/3 

 keep y x  year p_id
 
 

preserve

ren y y_nopbb
ren x x_nopbb 


joinby p_id using "kit_polid.dta" , unmatched(none)
foreach var in y_nopbb x_nopbb {
	egen std_`var'=std(`var')
}

save index_nopbb, replace




**************************************************************
****Add data on employment, income, education and family****
****from older time series (1980, 1985 and 1990) to create*****
****parental labor market experience index values***********
**************************************************************
clear all
cd  "\\micro.intra\Projekt\P0624$\P0624_Gem\Kitschelt\submission"

odbc load, exec("select p_id=LopNr, FamTyp, Yrke, SektorKod=Sektor, SNI5 from FoB_1980") dsn("P0624") clear
gen year=1980
duplicates drop p_id year, force
save gammalt_temp.dta, replace

odbc load, exec("select p_id=LopNr, FamTyp, Yrke80, Yrke85, SektorKod=Sektor, SNI5 from FoB_1985") dsn("P0624") clear
gen year=1985
duplicates drop p_id year, force
merge 1:1 p_id year using gammalt_temp, nogen
save gammalt_temp, replace

odbc load, exec("select p_id=Personnr_LopNr, HUtbSun from  HUtbSun_85") dsn("P0624") clear
gen year=1985
duplicates drop p_id year, force
merge 1:1 p_id year using gammalt_temp, nogen 
save gammalt_temp, replace

odbc load, exec(" select p_id=LopNr_PersonNr, FORETAGSINK_80 from IoT_1980") dsn("P0624") clear
gen year=1980
duplicates drop p_id year, force
merge 1:1 p_id year using gammalt_temp, nogen
save gammalt_temp, replace

odbc load, exec(" select p_id=LopNr_PersonNr, FORETAGSINK_85 from IoT_1985") dsn("P0624") clear
gen year=1985
duplicates drop p_id year, force
merge 1:1 p_id year using gammalt_temp, nogen
save gammalt_temp, replace

odbc load, exec("select SektorKod, Nyk, p_id=Person_LopNr, FamTypF, Civil, ForvErs, FInk90, Sun2000niva, AstSNI92  from P0624_Lisa_1990") dsn("P0624") clear
gen year=1990
duplicates drop p_id year, force
merge 1:1 p_id year using gammalt_temp, nogen
save gammalt_temp, replace


***Crosswalk occupational codes******
***from 1980 to 1996 classification***
***and crosswalk educational codes****

	foreach var in Yrke85 Yrke80 Yrke Nyk{
	destring `var', replace
	}

	joinby Yrke using "nyk-crosswalk.dta", unmatched(master)
	drop _merge
	replace Yrke85 = Yrke85ny if Yrke85ny!=.
	replace Yrke85 = Nyk if year==1990
	do ssyk_crosswalk
	do utb_crosswalk

	tostring ssyk96, replace
	replace ssyk96="0110" if ssyk96=="110"
	gen ssyk96kod = substr(ssyk96,1,3)
	gen ssyk_n = length(ssyk96)
	save gammalt_data_base, replace

	keep ssyk96 ssyk96kod
	drop if ssyk96==""
	gen count = 1
	collapse (sum) count, by(ssyk96 ssyk96kod)
	gen ssyk_n = length(ssyk96)
	gen sorting = 1-count
	sort ssyk96kod ssyk_n sorting
	replace ssyk96=ssyk96[_n+1] if ssyk96kod==ssyk96kod[_n+1] & ssyk_n==3
	keep if ssyk_n==3
	ren ssyk96 ssyk96_fix
	keep ssyk96_fix ssyk96kod
	save ssyk96_fix, replace

	use gammalt_data_base, replace
	merge m:1 ssyk96kod using ssyk96_fix
	replace ssyk96 = ssyk96_fix if ssyk_n==3
	drop ssyk96_fix _merge ssyk_n
	save gammalt_data_base, replace

	drop Nyk Yrke Yrke80 Yrke85ny Yrke85

***Add data for age, children and gender, generate age restriction***
	joinby p_id using "Birthyear_sex.dta", unmatched(master)
	drop _merge
	gen age = year - FodelseAr
	drop if age<18
	drop FodelseAr

***Add data for income and prepare sample restriction (income level above 3.5 of price base amount)***
	gen politician = ssyk96=="1110"
	duplicates drop p_id year, force
	merge 1:1 p_id year using forvers_79-89, nogen
	replace forvers=ForvErs*100 if year==1990
	replace forvers2=ForvErs*100 if year==1990
	replace pbb=29700 if year==1990
	drop ForvErs
	keep if year == 1980 | year == 1985 | year == 1990
	sort p_id year
	replace forvers=forvers[_n+1] if year==1980 & p_id==p_id[_n+1]
	replace forvers2=forvers2[_n+1] if year==1980 & p_id==p_id[_n+1]
	replace pbb = pbb[_n+1] if year==1980 & p_id==p_id[_n+1]

***Identify party employees
	gen party =  SNI5 == "93991" 
	gen interest = SNI5 == "93502"

	
	
	*******************
	******X-SCALE******
	*******************
	
***Generate income percentiles
	duplicates drop p_id year, force
	fasterxtile inc_pct=forvers if forvers>(pbb*3.5) & politician!=1, by(year) n(100)
	fasterxtile inc_pct2=forvers2 if forvers2>(pbb*3.5) & politician!=1, by(year) n(100)

***Create employment sector variable
	destring SektorKod, replace
	replace SektorKod=. if SektorKod==0 | SektorKod==99 
	gen PrivSec = 0 if SektorKod!=.
	replace PrivSec=1 if SektorKod==20 |SektorKod==30 |SektorKod==40 | SektorKod==60
	gen PubSec = (PrivSec-1)*-1
	gen PubCom = 0 if SektorKod!=.
	replace PubCom = 1 if SektorKod==29
	replace PubSec=0 if PubCom==1
	gen NonProfit=0  if SektorKod!=.
	replace NonProfit=1 if SektorKod==50
	drop SektorKod

	gen sector = .
	replace sector = PrivSec - PubSec - PubCom*0 - NonProfit*0
	replace sector = . if forvers<(pbb*3.5) |politician==1
	gen sector2 = sector
	replace sector2 = . if forvers2<(pbb*3.5) |politician==1

***Create capitalist-exectuive variable
	gen FInk = 0
	replace FInk = FInk90 if year ==1990
	replace FInk = FORETAGSINK_85/100 if year ==1985
	replace FInk = FORETAGSINK_80/100 if year ==1980
	drop FORETAGSINK_85 FORETAGSINK_80 FInk90
	gen capitalist = 0 if FInk!=.
	replace capitalist = 1 if FInk!=0 & FInk!=.

	gen vd_old = 0 if ssyk96!=""
	replace vd_old = 1 if ssyk96=="1210" & PrivSec==1

	gen cap_exec_old = . 
	replace cap_exec_old = -1 if capitalist==0
	replace cap_exec_old = 0.5 if capitalist!=0
	replace cap_exec_old = 0.7 if vd_old==1 
	replace cap_exec_old = 1 if capitalist!=0 & vd_old==1
	gen cap_exec_old2 = cap_exec_old 
	replace cap_exec_old = . if forvers<(pbb*3.5) |politician==1
	fasterxtile inc_pct_emp=forvers if forvers>(pbb*3.5) & cap_exec_old == -1 , by(year) n(100)
	replace   inc_pct_emp = (inc_pct_emp-50.5)/49.5
	replace  inc_pct_emp= inc_pct_emp/(2/1.5)-.25
	replace cap_exec_old = inc_pct_emp if cap_exec_old==-1

	replace cap_exec_old2 = . if forvers2<(pbb*3.5) |politician==1
	fasterxtile inc_pct_emp2=forvers2 if forvers2>(pbb*3.5) & cap_exec_old2 == -1 , by(year) n(100)
	replace   inc_pct_emp2 = (inc_pct_emp2-50.5)/49.5
	replace  inc_pct_emp2= inc_pct_emp2/(2/1.5)-.25
	replace cap_exec_old2 = inc_pct_emp2 if cap_exec_old2==1

***Impute data from previous years for low-income earners and politicians
	sort p_id year
	replace ssyk96="" if ssyk96=="0000" |ssyk96=="****" | ssyk96=="."

	foreach var in ssyk96{
		replace `var'="" if forvers<(pbb*3.5) |politician==1
		replace `var'=`var'[_n-1] if `var'=="" &  p_id==p_id[_n-1]
	}
	
	foreach var in cap_exec_old sector inc_pct{
		replace `var'=`var'[_n-1] if `var'==. &  p_id==p_id[_n-1]
	}

	foreach var of varlist PrivSec PubSec PubCom NonProfit{
		replace `var'=. if forvers<(pbb*3.5) |politician==1
		replace `var'=`var'[_n-1] if `var'==. &  p_id==p_id[_n-1]
	}

	foreach var in inc_pct {
	replace  `var' = (`var'-50.5)/49.5
	}

	foreach var in cap_exec_old2 sector2 inc_pct2{
		replace `var'=`var'[_n-1] if `var'==. &  p_id==p_id[_n-1]
	}

	foreach var of varlist PrivSec PubSec PubCom NonProfit{
		gen `var'2= `var'
		replace `var'2 =. if forvers2<(pbb*3.5) |politician==1
		replace `var'2=`var'2[_n-1] if `var'2==. &  p_id==p_id[_n-1]
	}

	foreach var in inc_pct2{
	replace  `var' = (`var'-50.5)/49.5
	}

***Add data on offshoring
	merge m:1 ssyk96kod using intcomp_ssyk.dta, nogen

	*******************
	******Y-SCALE******
	*******************	
	
***Create variable for years of education
	
	replace Sun2000niva="" if Sun2000niva=="*****" | Sun2000niva=="*" | Sun2000niva=="-"
	destring Sun2000niva, replace force
	replace  Sun2000niva=. if Sun2000niva>=999
	gen educ_year=.
	replace educ_year= 7.5 if Sun2000niva>=100 & Sun2000niva<200
	replace educ_year= 9.4 if Sun2000niva>=200 & Sun2000niva<300
	replace educ_year= 11.2 if Sun2000niva>=300 & Sun2000niva<330
	replace educ_year= 12.4 if Sun2000niva>=330 & Sun2000niva<400
	replace educ_year= 14.2 if Sun2000niva>=410& Sun2000niva<530
	replace educ_year= 17 if Sun2000niva>=530 & Sun2000niva<600
	replace educ_year= 20.4 if Sun2000niva>=600 & Sun2000niva<=999
	drop Sun2000niva

***Impute education data for 1980 from 1985
	sort p_id year
	replace educ_year = educ_year[_n+1] if p_id==p_id[_n+1] & year==1980
		
***Create categorical variable for education
	gen int eduyrs = educ_year
	drop educ_year
	gen education = .
	replace education = -1 if eduyrs<10 & eduyrs!=.
	replace education = -.5 if eduyrs==11 & eduyrs!=.
	replace education = -.25 if eduyrs==12 & eduyrs!=.
	replace education = 0.5 if eduyrs==14 & eduyrs!=.
	replace education = 1 if eduyrs>15 & eduyrs!=.
	replace education=. if eduyrs==.

***Add ONET-data on job content and generate variable used in the index
	drop ssyk96kod
	gen ssyk96kod = ssyk96
	merge m:1 ssyk96kod using SSYK96_Onet.dta, nogen

	foreach var of varlist ConMacProc DocRecInfo HandMoveObj InterComp AdminAct{
	gen `var'_ = 100-`var'
	}
	drop ConMacProc DocRecInfo HandMoveObj InterComp AdminAct
	egen galtan = rowmean(ConcOther AssCareOther NegConSolv ServOriCare SocPercep ConMacProc_ DocRecInfo_ HandMoveObj_ InterComp_ AdminAct_)
	drop ConcOther AssCareOther NegConSolv ServOriCare SocPercep ConMacProc_ DocRecInfo_ HandMoveObj_ InterComp_ AdminAct_ 
	replace galtan=. if ssyk96==""

***Create family status variable
	joinby p_id using "child_young_old.dta" , unmatched(master)
	gen child= birthyear_oldch<=year
	drop _merge birthyear_oldch birthyear_youch
	duplicates drop p_id year, force

	replace FamTyp="" if FamTyp==" "
	replace Civil="" if Civil!="G " & Civil!="S " & Civil!="OG"
	gen married=Civil=="G " | FamTyp=="1"
	gen partner=FamTypF=="11" | FamTypF=="21" |  ((FamTypF=="12" | FamTypF=="13"| FamTypF=="22" | FamTypF=="23") & child==1) | Civil=="S "
	gen mar_par= married==1 | partner==1
	gen cstatus = 1 if mar_par==0 & child==0
	replace cstatus = 0 if (mar_par==1 & child==0)|(mar_par==0 & child==1)
	replace cstatus = -1 if mar_par==1 & child==1
	drop Civil FamTypF FamTyp
	gen sex= 1 if Kon==2
	replace sex=-1 if Kon==1

	save gammalt_data, replace

***Prepare new data (2002-2014) to be matched with old data (1980-1990)
	use main_data_pop, replace
	gen sex= 1 if Kon==2
	replace sex=-1 if Kon==1
	keep p_id year party politician lowinc age inc_pct_emp  sector FamTypF Kon education Ssyk4 ssyk96kod PrivSec PasNar FInk inc_pct sex		galtan stdoffshore 

	gen  capitalist = 0 if FInk!=. & PasNar!=.
	replace capitalist = 1 if FInk!=0 & FInk!=. |  (PasNar)!=0 & (PasNar)!=.
	gen vd_old = 0 if Ssyk4!="" & Ssyk4!="****"
	replace vd_old = 1 if Ssyk4=="1210" & PrivSec==1
	gen cap_exec_old = . 
	replace cap_exec_old = -1 if capitalist==0
	replace cap_exec_old = 0.5 if capitalist!=0
	replace cap_exec_old = 0.7 if vd==1
	replace cap_exec_old = 1 if capitalist!=0 & vd_old==1
	replace cap_exec_old = inc_pct_emp if cap_exec_old==-1

	gen inc_pct2=inc_pct
	gen lowinc2=lowinc
	gen cap_exec_old2 = cap_exec_old
	gen sector2 = sector

	keep politician lowinc lowinc2 age p_id year cap_exec_old cap_exec_old2 sector sector2 education galtan stdoffshore Ssyk4 ssyk96kod sex	inc_pct inc_pct2
	duplicates drop p_id year, force
	save xyscale_old, replace

***Merge old and new data
	use gammalt_data, clear
	gen lowinc= forvers<(pbb*3.5)
	gen lowinc2= forvers2<(pbb*3.5)

	keep  p_id age party lowinc lowinc2 politician year cap_exec_old cap_exec_old2 sector education galtan stdoffshore inc_pct inc_pct2 sex	ssyk96kod ssyk96 sector2
	
	append  using xyscale_old

***Generate additional demographic variables and normalize others to range from -1 to 1.

	replace ssyk96=Ssyk4 if missing(ssyk96)
	drop Ssyk4
	drop if year==.

	su stdoffshore, meanonly
	gen norm_offshore = (stdoffshore - r(min))/(r(max)-r(min))
	replace norm_offshore = (norm_offshore-0.5)*2

	su galtan, meanonly
	gen norm_galtan = (galtan - r(min))/(r(max)-r(min))
	replace norm_galtan = (norm_galtan-0.5)*2


	foreach var in    stdoffshore galtan{
	fastxtile `var'_pct = `var', n(100)
	replace  `var'_pct = (`var'_pct-50.5)/49.5
	}

	
***Generate various types of x and y variables for the whole dataset
	gen x= stdoffshore_pct/3 + sector/3 + cap_exec_old/3
	gen x_alt = inc_pct/3 + sector/3 + cap_exec_old/3

	gen x2= stdoffshore_pct/3 + sector2/3 + cap_exec_old2/3
	gen x_alt2 = inc_pct2/3 + sector2/3 + cap_exec_old2/3

	gen y = galtan_pct/2 + education/4 + sex/4

	save nytt_gammalt, replace
*******************************************************************************
*Create variable for the most common party someone hase belonged to****
*****It is rare for politicians to have belonged to more than one party****
***************************************************************************
use "\\micro.intra\Projekt\P0624$\P0624_Gem\Data Extraction and Files\Data files\politicians combined.dta" 
replace parti_initial=parti_initial_ri if parti_initial==""
replace parti_initial=parti_initial_lt if parti_initial==""
drop if wrlist_app==1

bysort p_id: egen party=mode(parti_initial)

keep party p_id
duplicates drop p_id, force
save "mode_party.dta", replace

***First define the income percentiles for 1979in the whole population	
clear



odbc load, exec("select * from IoT_1980") dsn("P0624") clear

ren LopNr_PersonNr p_id 
gen year =1980
keep DISPINK LONEINK FORETAGSINK FORVINK p_id year
foreach var in DISPINK LONEINK FORETAGSINK FORVINK {
	ren `var'_80  `var' 
}
save temp, replace


odbc load, exec("select * from IoT_1985") dsn("P0624") clear
ren LopNr_PersonNr p_id 
gen year =1985
keep DISPINK LONEINK FORETAGSINK FORVINK p_id year
foreach var in DISPINK LONEINK FORETAGSINK FORVINK {
	ren `var'_85  `var' 
}
append using temp

save temp, replace

odbc load, exec("select  * from P0624_Lisa_1990") dsn("P0624") clear

keep Person_LopNr FInk90 ForvErs DispInk LoneInk
ren Person p_id 

gen year =1990
gen forvink = Lone+FInk
drop Lone FInk
append using temp
duplicates drop p_id  year, force
save temp, replace


joinby p_id using "\\micro.intra\Projekt\P0624$\P0624_Gem\Data Extraction and Files\Data files\Birthyear_sex.dta" , unmatched(master)
	 
ren FodelseAr birthyear 
ren Kon sex
gen winc= FORVINK if year<1990
replace winc= ForvErs if year==1990

gen inc= DISPINK if year<1990
replace inc=  DispInk if year==1990


foreach var of varlist  inc winc{ 
fasterxtile `var'_pct=`var', by( birthyear year sex)  n(100)
}
gen age =year-birthyear
keep p_id year inc_pct winc_pct sex age

keep if age>=18
joinby year p_id using "nytt_gammalt.dta", unmatched(master)
keep p_id year inc_pct winc_pct sex age x y


joinby p_id using "\\micro.intra\Projekt\P0624$\P0624_Gem\Data Extraction and Files\Data files\parents_long.dta", unmatched(master)
 
 cd  "\\micro.intra\Projekt\P0624$\P0624_Gem\Kitschelt\submission"


keep  pol_id p_id parent year  x y  inc_pct winc_pct age 
replace year =year-1900
foreach var of varlist  x y  inc_pct winc_pct age{
	ren `var' `var'_
}


duplicates drop p_id parent pol_id year, force
reshape wide  x_ y_  inc_pct_ winc_pct_ age_, i(p_id parent pol_id) j(year)

joinby  p_id using "mode_party.dta", unmatched(master)
 


foreach var of varlist  inc_pct_80- y_90 party{
	ren `var' `var'_
}
drop if parent==""
drop _merge p_id 
reshape wide inc_pct_80_-  party_, i(pol_id) j(parent) string

ren pol_id p_id
joinby p_id using "kit_polid.dta" , unmatched(none)
save parents_kit, replace

***Files for poor and rich parents
	
 	odbc load, exec("select p_id=LopNr_PersonNr, inc=DISPINK_79 from IoT_1979") dsn("P0624") clear
	gen year=1979
	save temp_inc, replace
	
		foreach year in 80 81 82 83 84 85 86 87 88 89 {
	
		odbc load, exec("select p_id=LopNr_PersonNr, inc=DISPINK_`year' from IoT_19`year'") dsn("P0624") clear
			
			gen year = 19`year'
			append using temp_inc
		save temp_inc, replace
		}
		
			
	foreach year in 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 {
		
		odbc load, exec("select p_id=Person_LopNr, inc=DispInk  , inc_f =DispInkPersF from P0624_Lisa_`year'") dsn("P0624") clear
				gen year =`year'
			append using temp_inc
		save temp_inc, replace

	}
	
	foreach year in 2004 2005 2006 2007 2008 2009 2010 2011 2012{
			odbc load, exec("select p_id=Person_LopNr, inc=DispInk04, inc_f =DispInkPersF04 from P0624_Lisa_`year'") dsn("P0624") clear
				gen year =`year'
			append using temp_inc
		save temp_inc, replace
		
	}	
	
	
foreach var in inc inc_f{
replace `var'= `var'/100 if year<1990
ren `var' `var'_real
replace `var'_real= `var'_real/0.3376 if year==1979
replace `var'_real= `var'_real/0.3836 if year==1980
replace `var'_real= `var'_real/0.43  if year==1981
replace `var'_real= `var'_real/0.4668 if year==1982
replace `var'_real= `var'_real/0.5085 if year==1983
replace `var'_real= `var'_real/0.5492 if year==1984
replace `var'_real= `var'_real/0.5897 if year==1985
replace `var'_real= `var'_real/0.6146 if year==1986
replace `var'_real= `var'_real/0.6406 if year==1987
replace `var'_real= `var'_real/0.6779 if year==1988
replace `var'_real= `var'_real/0.7214 if year==1989
replace `var'_real= `var'_real/0.795905 if year==1990
replace `var'_real= `var'_real/0.8715 if year==1991
replace `var'_real= `var'_real/0.891446 if year==1992
replace `var'_real= `var'_real/0.932873 if year==1993
replace `var'_real= `var'_real/0.953203 if year==1994
replace `var'_real= `var'_real/0.977369 if year==1995
replace `var'_real= `var'_real/0.981972 if year==1996
replace `var'_real= `var'_real/0.986958 if year==1997
replace `var'_real= `var'_real/0.985807 if year==1998
replace `var'_real= `var'_real/0.990027 if year==1999
replace `var'_real= `var'_real/1 if year==2000
replace `var'_real= `var'_real/1.024549 if year==2001
replace `var'_real= `var'_real/1.046414 if year==2002
replace `var'_real= `var'_real/1.066743 if year==2003
replace `var'_real= `var'_real/1.070963 if year==2004
replace `var'_real= `var'_real/1.075566 if year==2005
replace `var'_real= `var'_real/1.090219 if year==2006
replace `var'_real= `var'_real/1.114346 if year==2007
replace `var'_real= `var'_real/1.153088 if year==2008
replace `var'_real= `var'_real/1.149444 if year==2009
replace `var'_real= `var'_real/1.1641 if year==2010
replace `var'_real= `var'_real/1.1945 if year==2011
replace `var'_real= `var'_real/1.205 if year==2012
}
	
	
save temp_inc, replace
drop inc_f_real
 compress

joinby p_id year using  "\\micro.intra\Projekt\P0624$\P0624_Gem\Data Extraction and Files\Data files\famid_year.dta", unmatched(master)

drop _merge
bysort famyear_id year: egen incf=mean (inc_real)
replace inc_real=incf if famyear_id!=.
drop incf
	joinby p_id using "Birthyear_sex.dta", unmatched(master) 
	drop _merge Kon
		
	gen age = year-FodelseAr
	drop if age<18
	drop Fodel
drop age famyear_id
	bysort year : egen inc_med=median(inc)

gen poor=inc_real<(.6*inc_med) & inc_real!=.
compress
	bysort year: egen p90 = pctile(inc_real), p(90)
gen rich=inc_real>p90 & inc_real!=.

save temp_inc, replace
use kit_polid.dta, clear
joinby p_id using "Birthyear_sex.dta", unmatched(master) 
drop _merge
expand 4
bysort p_id:egen rank=rank(_n)
gen year=Fodelse+14+rank
drop if year>2012| year<1979
ren p_id pol_id
joinby pol_id using "\\micro.intra\Projekt\P0624$\P0624_Gem\Data Extraction and Files\Data files\parents_long.dta", unmatched(none)

joinby p_id year using temp_inc, unmatched(none)

gen count=1
gen count_m=1 if parent=="mother"
gen count_f=1 if parent=="father"
gen poor_m=poor  if parent=="mother"
gen poor_f=poor  if parent=="father"
gen rich_m=rich  if parent=="mother"
gen rich_f=rich  if parent=="father"
collapse (sum) count* poor* rich*, by(pol_id)
ren pol_id p_id
gen poor_par = poor>0 if poor!=. & count==8
gen rich_par = rich_m==4 |rich_f==4  if rich!=. & count==8
save poor_parent, replace


clear all
cd  "\\micro.intra\Projekt\P0624$\P0624_Gem\Kitschelt\submission"


*******Oesch
use main_data_pop, replace

merge m:1 Ssyk4 using "ssyk4 oesch"
destring Ssyk4, replace
gen count = 1 if ForvErs>(pbb*3.5)
bysort year o_nr: egen ftgsize = sum(count)
replace ftgsize=0 if missing(o_nr)

replace oesch = 1 if YrkStalln == "4" & ftgsize > 9
replace oesch = 1 if YrkStalln == "5" & ftgsize > 9
replace oesch = 2 if YrkStalln == "5" & ftgsize ==1 & Ssyk4 > 2000 & Ssyk4 <2229
replace oesch = 2 if YrkStalln == "5" & ftgsize ==1 & Ssyk4 > 2300 & Ssyk4 <2470
replace oesch = 2 if YrkStalln == "4" & ftgsize ==1 & Ssyk4 > 2000 & Ssyk4 <2229
replace oesch = 2 if YrkStalln == "4" & ftgsize ==1 & Ssyk4 > 2300 & Ssyk4 <2470
replace oesch = 3 if YrkStalln == "5" & ftgsize <10 & ftgsize>1
replace oesch = 3 if YrkStalln == "4" & ftgsize <10 & ftgsize>1
replace oesch = 4 if YrkStalln == "5" & ftgsize ==1 & oesch !=2
replace oesch = 4 if YrkStalln == "4" & ftgsize ==1 & oesch !=2

keep oesch p_id year
joinby p_id using "kit_polid.dta" , unmatched(none)
save  oesch, replace

**********************************************************
******Define EGP Classification for each indiviual********
**********************************************************

use scale_, clear
compress
joinby p_id using Birthyear_sex.dta, unmatched(none)

gen age = year - FodelseAr
drop if  age<18
gen pbb=.

replace	pbb	=	44000	if 	year 	==	2012
replace	pbb	=	42800	if 	year 	==	2011
replace	pbb	=	42400	if 	year 	==	2010
replace	pbb	=	42800	if 	year 	==	2009
replace	pbb	=	41000	if 	year 	==	2008
replace	pbb	=	40300	if 	year 	==	2007
replace	pbb	=	39700	if 	year 	==	2006
replace	pbb	=	39400	if 	year 	==	2005
replace	pbb	=	39300	if 	year 	==	2004
replace	pbb	=	38600	if 	year 	==	2003
replace	pbb	=	37900	if 	year 	==	2002
replace	pbb	=	36900	if 	year 	==	2001
replace pbb=pbb/100 
gen politician =  AstSNI2007 == "84111" | AstSNI2002 == "75111" | AstSNI92 == "75111"






replace Ssyk4="" if Ssyk4=="0000" |Ssyk4=="****" 
sort p_id year
foreach var in AstSNI2007 AstSNI2002 AstSNI92 Ssyk4{
	replace `var'="" if ForvErs<(pbb*3.5) |politician==1
	replace `var'=`var'[_n-1] if `var'=="" &  p_id==p_id[_n-1]
}
keep if year == 2006 | year == 2010 | year == 2012 

rename AstSNI2002 astsni2002
rename AstSNI2007 astsni2007

mmerge   astsni2007 using "key_sni07_sni02", type(n:1) unmatch(master) missing(nomatch)
destring astsni2002, replace 
destring modsni2002, replace
replace astsni2002 = modsni2002 if year>2007 & astsni2002==.


gen sni2_02 = trunc(astsni2002/100)



// 2002-
recode sni2_02 (150/379=1)(.=.)(else=0)					      , gen(manufacturing02)
recode sni2_02 (500/749 900/909 920/939=1)(.=.)(else=0)		  , gen(priv_service02)
recode sni2_02 (750/759 800/809 850/859=1)(.=.)(else=0)		  , gen(pub_service02)
gen elseind02 = 0 if sni2_02!=.
replace elseind02 = 1 if manufacturing02==0 & priv_service02==0 & pub_service02==0

foreach type in manufacturing priv_service  pub_service elseind {
	gen `type'  =.
	replace `type' = `type'02		if year>=2002
}

gen industry = manufacturing*1 + priv_service*2 + pub_service*3+elseind*4
label define industry 1 "Manufacturing" 2 "Private service" 3 "Public service"  4 "Else industry"
label values industry industry
tab year industry
gen ssyk3=substr(Ssyk4, 1, 3)
destring ssyk3, replace force 
mmerge ssyk3 industry using "isco_sei_key_2", type(n:1) unmatched(master) missing(nomatch) umatch(isco3 industry)

keep  modsei p_id year industry
joinby p_id using "kit_polid.dta" , unmatched(none)
save sei_own, replace


********************************************
******Factor Analysis used for Alternative Index************
**********************************************************
use main_data_pop,clear

drop if year <2006
drop if x==. |y==.

gen sex =1 if Kon==2
replace sex=-1 if Kon==1
global lablist education  sex    ConcOther AssCareOther NegConSolv ServOriCare SocPercep ///
   ConMacProc_ DocRecInfo_ HandMoveObj_ InterComp_ AdminAct_ ///
 stdoffshore_pct  /// 
  PrivSec-NonProfit inc_pct  busink OpFtgLedare BusPers 
  
  pca $lablist
  
  screeplot , yline(1)
  graph save screeplot, replace
  estat loadings
  estat kmo 
  predict pc1 pc2 pc3 , score
  
  
  gen valar=year
replace valar=2014 if year==2012
  foreach pc in pc1 pc2 pc3{
  egen std_`pc'=std(`pc')
  }
 keep std_pc* p_id year
 
 joinby p_id using "kit_polid.dta" , unmatched(none)
 save pc_index, replace
  *Create index for siblings
  
odbc load, exec(" select *  from  BioSyskon") dsn("P0624") clear
ren LopNr p_id
joinby p_id using "kit_polid.dta" , unmatched(none)
duplicates drop p_id , force
ren p_id pol_id
ren LopNr_BioSyskon p_id
joinby p_id  using "main_data_pop", unmatched(none)
collapse(mean) x y, by(pol_id year)
ren x sib_x
ren y sib_y
ren pol_id p_id
save sib_index, replace


****************************************************************************
************************Create dataset with parental ids *****************
*****************************************************************************

odbc load, exec(" select p_id=LopNr, LopNr_BioMor, LopNr_BioFar  from  BioForaldrar") dsn("P0624") clear

joinby p_id using "kit_polid.dta" , unmatched(none)
duplicates drop p_id, force


save parent_id, replace

**********************************************************************
**************Labor market experiences and party affiliation of parents*****************
******************************************************************************
***First define the income percentiles for 1979in the whole population	
odbc load, exec("select * from IoT_1980") dsn("P0624") clear

ren LopNr_PersonNr p_id 
gen year =1980
keep DISPINK LONEINK FORETAGSINK FORVINK p_id year
foreach var in DISPINK LONEINK FORETAGSINK FORVINK {
	ren `var'_80  `var' 
}
save temp, replace


odbc load, exec("select * from IoT_1985") dsn("P0624") clear
ren LopNr_PersonNr p_id 
gen year =1985
keep DISPINK LONEINK FORETAGSINK FORVINK p_id year
foreach var in DISPINK LONEINK FORETAGSINK FORVINK {
	ren `var'_85  `var' 
}
append using temp

save temp, replace

odbc load, exec("select  * from P0624_Lisa_1990") dsn("P0624") clear

keep Person_LopNr FInk90 ForvErs DispInk LoneInk
ren Person p_id 

gen year =1990
gen forvink = Lone+FInk
drop Lone FInk
append using temp
duplicates drop p_id  year, force
save temp, replace


joinby p_id using "\\micro.intra\Projekt\P0624$\P0624_Gem\Data Extraction and Files\Data files\Birthyear_sex.dta" , unmatched(master)
	 
ren FodelseAr birthyear 
ren Kon sex
gen winc= FORVINK if year<1990
replace winc= ForvErs if year==1990

gen inc= DISPINK if year<1990
replace inc=  DispInk if year==1990


foreach var of varlist  inc winc{ 
fasterxtile `var'_pct=`var', by( birthyear year sex)  n(100)
}
gen age =year-birthyear
keep p_id year inc_pct winc_pct sex age

keep if age>=18
joinby year p_id using "\\micro.intra\Projekt\P0624$\P0624_Gem\Kitschelt\nytt_gammalt.dta", unmatched(master)
keep p_id year inc_pct winc_pct sex age x y


joinby p_id using "\\micro.intra\Projekt\P0624$\P0624_Gem\Data Extraction and Files\Data files\parents_long.dta", unmatched(master)
 

keep  pol_id p_id parent year  x y  inc_pct winc_pct age 
replace year =year-1900
foreach var of varlist  x y  inc_pct winc_pct age{
	ren `var' `var'_
}


duplicates drop p_id parent pol_id year, force
reshape wide  x_ y_  inc_pct_ winc_pct_ age_, i(p_id parent pol_id) j(year)

joinby  p_id using "\\micro.intra\Projekt\P0624$\P0624_Gem\Kitschelt\mode_party.dta", unmatched(master)
 


foreach var of varlist  inc_pct_80- y_90 party{
	ren `var' `var'_
}
drop if parent==""
drop _merge p_id 
reshape wide inc_pct_80_-  party_, i(pol_id) j(parent) string

ren pol_id p_id
save parents_kit, replace


****************************************************************************
************************Create dataset with region of birth*****************
*****************************************************************************
odbc load, exec(" select *  from FodelseVarldsdel") dsn("P0624") clear
ren LopNr p_id	
gen inv1g_ejsv = FodelseVarldsdel !="Sverige" if FodelseVarldsdel!=""
gen inv1g_ejn = FodelseVarldsdel !="Sverige" & FodelseVarldsdel !="Norden utom Sverige" if FodelseVarldsdel!=""
gen inv1g_eje = FodelseVarldsdel !="Sverige" & FodelseVarldsdel !="Norden utom Sverige" ///
	&  FodelseVarldsdel !="EU28 utom Norden" &  FodelseVarldsdel !="Nordamerika"	 if FodelseVarldsdel!=""
joinby p_id using kit_polid, unmatched(none)
save immigrants, replace

log close